package control.control;

import base.standard.constant.ErrorCode;
import base.standard.exception.SimpleException;
import base.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import control.module.vo.HealthVO;
import base.bean.StandardResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

/**
 * Health control
 *
 * @description: 健康检查接口
 * @author: cyj
 * @date 2020 -10-15 17:23:35
 */
@RestController
@RequestMapping("/health")
@ResponseStatus(code = HttpStatus.OK)
@Api(tags = "健康检查")
@Slf4j
public class HealthControl {

    /**
     * Health standard response
     *
     * @return the standard response
     * @description: 获取服务信息
     */
    @GetMapping("/info")
    @ApiOperation("获取服务信息")
    @Cacheable(cacheNames = "health")
    public StandardResponse health() {
        HealthVO healthVO = new HealthVO().setName("simple").setVersion("1.0");
        log.info("获取服务信息");
        return ResponseUtil.success(healthVO);
    }

    @GetMapping("/clear")
    @CacheEvict(cacheNames = "health")
    public StandardResponse clearHealth() {
        return ResponseUtil.success(null);
    }

    /**
     * Error 抛出异常响应
     *
     * @param name name
     */
    @GetMapping("/error")
    @ApiOperation("获取错误")
    public void error(String name) {
        throw new SimpleException(ErrorCode.UNKNOW_ERROR);
    }

}
